(function(){
    App.views.RegisterView = Backbone.View.extend({
        el: '#register-menu',
        template:  _.template($('#tpl-proc-register').html()),      
        events: {
            'click .btn-confirm': 'submit',
            'click .btn-reset': 'clear',
            'hidden.bs.modal': 'postHide'
        },
        map: {
            name:       '#proc-name',
            owner:      '#proc-owner',
            psStr:      '#proc-psstr',
            ports:      '#proc-ports',
            path:       '#proc-path',
            startCmd:   '#proc-startcmd',
            remark:     '#proc-remark',
        },
        initialize: function(options){
            this.eventBus = options.eventBus;
            this.watch();
        },
        watch: function(){
            // 模型更新完成后通知主视图处理后续操作
            this.listenTo(this.model, 'requestSuccess', function(){
                this.eventBus.trigger('requestSuccess');
            });
        },
        submit: function(){
            var opts = {
                minionId: App.xbox.minionId
            };
            for(var i in this.map){
                opts[i] = this.$el.find(this.map[i]).val();
            }
            this.model.set(opts).create();
        },
        postHide: function(){
            // 注册框隐藏后, 重写url记录当前页面状态
            var url = [
                '#process/',
                App.xbox.minionId,
            ].join(''); 
            App.router.navigate(url, {trigger: false}); 
        },
        render: function(procInfo){
            this.model.set(procInfo);
            this.$el.find('.modal-body').html(this.template(this.model.toJSON()));
            this.$el.modal('show');
        },
        hide: function(){
            this.$el.modal('hide');
        },
    });
})();
